package com.aiti.lulian.service.newCard;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.entity.newCard.CardComponentData;
import com.aiti.lulian.entity.newCard.CardEnterpriseComponent;
import com.aiti.lulian.mapper.newCard.CardComponentDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-08-28 15:57
 **/
@Service
@Slf4j
public class CardComponentDataService extends BaseSerivceImpl<CardComponentDataMapper, CardComponentData> {


    @Transactional(rollbackFor = Exception.class)
    // 创建名片的组件部分数据
    public void createCardComponentData(List<CardEnterpriseComponent> templateComponentData, String cardId) {
        if(CollectionUtil.isEmpty(templateComponentData)) {
            return;
        }
        List<CardComponentData> componentDataList = new ArrayList<>();
        for(CardEnterpriseComponent templateComponent : templateComponentData) {
            CardComponentData componentData = new CardComponentData();
            componentData.setBaseId(IdUtil.simpleUUID());
            componentData.setCardId(cardId);
            componentData.setTemplateComponentId(templateComponent.getBaseId());
            componentData.setComponentMetaData(templateComponent.getComponentMetaData());
            componentData.setSort(templateComponent.getSort());

            componentDataList.add(componentData);
        }
        this.saveBatch(componentDataList);
    }

}
